package com.howie.parnote.web.front;

import com.howie.parnote.base.EnumModelAndView;
import com.howie.parnote.constant.RespMessageConstant;
import com.howie.parnote.constant.ViewConstant;
import com.howie.parnote.exception.InvalidParameterException;
import com.howie.parnote.module.NoteModule;
import com.howie.parnote.module.SharerModule;
import com.howie.parnote.module.interaction.SignupSharer;
import com.howie.parnote.util.Page;
import com.howie.parnote.util.PageUtil;
import com.howie.parnote.util.SharerUtil;
import com.howie.parnote.web.ParnoteBaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * <p>Title: parnote</p>
 * <p>
 * Description: parnote
 * </p>
 *
 * @author huangyan
 *         Created by HY on 2015/4/16 0016.
 * @version v1.0.0
 */
@Controller
public class FrontAccessController extends ParnoteBaseController {

	@RequestMapping(value = "/")
	public ModelAndView index(HttpServletRequest request, HttpSession session) {
		ModelAndView mav;
		String email = "testc71187@qq.com";
		String psw = "dongwx1881";
		try {
			sharerService.login(email, psw, session);
		} catch (Exception e) {
			e.printStackTrace();
		}
		SharerModule sharer = SharerUtil.getSharer(session);
		if (sharer == null) {
			mav = new EnumModelAndView(ViewConstant.VIEW_PUBLIC_INDEX);
		} else {
			mav = new EnumModelAndView(ViewConstant.VIEW_INDEX);
			try {
				Page<NoteModule> page = new Page<NoteModule>(PageUtil.PageSize.DEFAULT_SIZE);
				Map<String, Integer> pageParams = PageUtil.init(page, request);
				noteService.selectByPage(page, pageParams);
				mav.addObject("page", page);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return mav;
	}

	@RequestMapping(value = "/signup", method = RequestMethod.POST)
	public ModelAndView signupPostView(SignupSharer signupSharer, RedirectAttributes redirectAttributes) {
		ModelAndView mav = new EnumModelAndView(ViewConstant.VIEW_SIGNUP);
		if (!signupSharer.isEmpty()) {
			redirectAttributes.addFlashAttribute(signupSharer);
		}
		return mav;
	}

	@RequestMapping(value = "/signup", method = RequestMethod.GET)
	public ModelAndView signupGetView(@RequestParam(required = false) SignupSharer signupSharer) {
		ModelAndView mav = new EnumModelAndView(ViewConstant.VIEW_SIGNUP);
		mav.addObject("signupSharer", signupSharer);
		return mav;
	}

	@RequestMapping(value = "/action/signup")
	public String signup(SignupSharer signupSharer, RedirectAttributes redirectAttributes) {
		try {
			sharerService.insertSharer(signupSharer);
		} catch (InvalidParameterException e) {
			redirectAttributes.addFlashAttribute(signupSharer);
			return ViewConstant.REDIRECT.getPath() + "signup";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ViewConstant.REDIRECT.getPath() + "/";
	}

	@RequestMapping(value = "/login")
	public String login() {
		return ViewConstant.VIEW_LOGIN.getPath();
	}

	@RequestMapping(value = "/action/login")
	public String login(String email, String password, HttpSession session) {
		try {
			sharerService.login(email, password, session);
			return ViewConstant.REDIRECT.getPath() + "/";
		} catch (InvalidParameterException e) {
			setRespMsg(RespMessageConstant.ILLEGAL_ARGUMENT);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			setRespMsg(RespMessageConstant.SYSTEM_ERROR);
		}
		return ViewConstant.REDIRECT.getPath() + "login";
	}

	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		SharerUtil.removeSharerFromSession(session);
		return ViewConstant.REDIRECT.getPath() + "/";
	}


}
